package calao.plan;

import calao.plan.client.Task;
import calao.storage.DataBinding;
import calao.storage.DataReader;
import calao.storage.DataWriter;
import calao.storage.UnknownBindingException;

public class TypeDataBinding<T extends Task> 
extends DataBinding<T> 
{

	final TaskType<T> type;
	
	public TypeDataBinding(TaskType<T> type, Class<T> jtype, String name) 
	{
		super(jtype, name);
		this.type = type;
	}
	
	@Override
	public T readObject(DataReader reader, int version)
	throws UnknownBindingException 
	{
		try {
			T object = type.binding.getDataType().newInstance();
			type.readTypeData(reader, object);
			return object;
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void write(DataWriter writer, T object) 
	{
		type.writeTypeData(writer, object);
	}

}
